package com.jzg.mvc.api.cabinet;

import java.util.List;


import com.jfinal.core.Path;
import com.jfinal.kit.StrKit;
import com.jzg.common.controller.BaseController;
import com.jzg.common.model.biz.CabModule;
import com.jzg.common.model.biz.CabModuleBoard;
import com.jzg.common.model.biz.CabModuleFaultBox;
import com.jzg.common.util.UuidUtil;


@Path(value = "/api/cab/fault", viewPath = "")
public class FaultController extends BaseController{
	
	public void listData() {
		List<CabModuleFaultBox> cabModuleFaultBox = CabModuleFaultBox.dao.find("SELECT O.*, "
				+ "(SELECT name FROM biz_cab_module WHERE ID=O.moduleId ) AS moduleName,"
				+ "(SELECT serialNo FROM biz_cab_module_board WHERE ID=O.boardId ) AS serialNo from "+CabModuleFaultBox.TABLE_NAME+" O WHERE CABID=? AND STATUS='0' ",this.get("cabId"));
		renderSuccess("查询成功！", cabModuleFaultBox);
	}
	
	/***
	 * 保存
	 */
	public void save() {
		CabModuleFaultBox cabModuleFaultBox = getModel(CabModuleFaultBox.class);
		if (StrKit.notBlank(cabModuleFaultBox.getId())) {
			if (cabModuleFaultBox.update()) {
				renderSuccess("更新成功！");
			} else {
				renderError("更新失败！");
			}
		} else {
			cabModuleFaultBox.setId(UuidUtil.getUUID());
			cabModuleFaultBox.setStatus("0");
			if (cabModuleFaultBox.save()) {
				renderSuccess("保存成功！");
			} else {
				renderError("保存失败！");
			}
		}

	}
	
	/***
	 * 删除
	 * @throws Exception
	 */
	public void delete() throws Exception {
		String id = getPara("id");
		CabModuleFaultBox cabModuleFaultBox = CabModuleFaultBox.dao.findById(id);
		if (cabModuleFaultBox != null) {
			cabModuleFaultBox.setStatus("1");
			if (cabModuleFaultBox.update()) {
				renderSuccess("删除成功！");
			} else {
				renderError("删除失败！");
			}
		} else {
			renderError("数据不存在,请刷新后再试!");
		}
	}
	
	public void getSelectData() {
		String cabId = getPara("cabId");
		List<CabModule>  modules = CabModule.dao.find("select * from "+CabModule.TABLE_NAME+" where cabId=? order by sort ",cabId);
		for (CabModule cabModule : modules) {
			List<CabModuleBoard> board = CabModuleBoard.dao.find("select * from "+CabModuleBoard.TABLE_NAME+" where moduleId=? order by serialNo ",cabModule.getId());
			cabModule.setBoards(board);
		}
		renderSuccess("查询成功！", modules);
	}
	
	
}
